package com.shop.shoporder.wapController;

import com.shop.shoporder.common.MessageConstants;
import com.shop.shoporder.common.Response;
import com.shop.shoporder.common.StringUtil;
import com.shop.shoporder.entity.*;
import com.shop.shoporder.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@Controller
@RequestMapping("wap")
public class GroupbuyDetailController {

    @Autowired
    GroupbuyGoodService groupbuyGoodService;
    @Autowired
    WapGoodsAndSpecService wapGoodsAndSpecService;
    @Autowired
    WapGoodsDetailsService wapGoodsDetailsService;
    @Autowired
    GroupbuyService groupbuyService;
    @Autowired
    GroupbuyFinaceService groupbuyFinaceService;

    Logger logger = LoggerFactory.getLogger(GroupbuyDetailController.class);

    /**
     * 拼团详情
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/getGroupbuyDetail")
    public Response getGroupbuyDetail(HttpServletRequest request) {

        Map<String, Object> map = new HashMap<>();
        int groupbuygoodId = 0;
        int goodId = 0;
        int specId = 0;
//        int userNoId = 0;
        int groupbuyId = 0;
//        String token = request.getHeader("token");
        String groupbuyIds = request.getParameter("groupbuyId");
        String groupbuygoodIds = request.getParameter("groupbuygoodId");

        if (groupbuygoodIds != null && !"".equalsIgnoreCase(groupbuygoodIds) && StringUtil.isNumeric(groupbuygoodIds)) {
            groupbuygoodId = Integer.parseInt(groupbuygoodIds);
        }
        if (groupbuyIds != null && !"".equalsIgnoreCase(groupbuyIds) && StringUtil.isNumeric(groupbuyIds)) {
            groupbuyId = Integer.parseInt(groupbuyIds);
        }

        GroupbuyGood groupbuyGood = null;
        groupbuyGood = groupbuyGoodService.getGroupbuyGood(groupbuygoodId);
        if (groupbuyGood != null) {
            goodId = groupbuyGood.getGoodsId();
            specId = groupbuyGood.getSpecId();

        }

        GroupbuyGoodInfo groupbuyGoodInfo = null;
        if (goodId != 0 && specId != 0) {
            groupbuyGoodInfo = groupbuyGoodService.getGoodInfoByGoodIdAndSpecId(goodId, specId);
        }
        //参与团购查询产品规格列表
        GroupbuyGoodDetail groupbuyGoodDetail = null;
        List<GroupbuySpec> specList = null;
        groupbuyGoodDetail = wapGoodsDetailsService.getGroupbuyGoodDetail(goodId);
        if (groupbuyGoodDetail != null) {
            specList = groupbuyGoodDetail.getGroupbuySpecs();
        }

        int groupbuyTotalCount = 0;

        //团购信息
        List<GroupbuyInfo> groupbuyInfoList = null;
        groupbuyInfoList = groupbuyService.getGroupbuyAndParticpaterInfo(groupbuyId);
        if (!groupbuyInfoList.isEmpty()) {
            for (int i = 0; i < groupbuyInfoList.size(); i++) {
                groupbuyTotalCount = groupbuyInfoList.get(i).getParticipaters().size() + 1;
            }
        }


        //团购人数等级数据
        List<GroupbuyFinace> groupbuyFinaceList = null;
        groupbuyFinaceList = groupbuyFinaceService.getGroupbuyFinaceList();


        //单团最大团购人数
        GroupbuyFinace groupbuyFinace = null;
        int maxOneGroupbuyCount = 0;
        groupbuyFinace = groupbuyFinaceService.getMaxGroupbuyCount();
        if (groupbuyFinace != null) {
            maxOneGroupbuyCount = groupbuyFinace.getGroupbuyCount();
        }


        map.put("groupbuyGoodInfo", groupbuyGoodInfo);//产品信息
        map.put("groupbuyInfoList", groupbuyInfoList);//团购用户信息
        map.put("groupbuyFinaceList", groupbuyFinaceList);//团购达到的等级
        map.put("maxOneGroupbuyCount", maxOneGroupbuyCount);//最大团人数
        map.put("specList", specList);//产品规格列表
        map.put("groupbuyTotalCount", groupbuyTotalCount);
        return new Response(MessageConstants.MSG_SUCCESS, map, 0, "", request);
    }


}
